package com.token.sysorg.services;
import java.sql.ResultSet;
import com.token.sql.IntegrateSql;
import com.token.sql.PrepareSql;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.token.base.FuncUtil;
import com.token.base.Pl;
/**
 * 读组织架构_返回tree的json结构
 * @author anger
 * @version 1.0
 */
public class FSysOrgRead_ForTree_All{
	public JSONArray ReadOrg_ForTree(FuncUtil F,IntegrateSql iSql) throws Exception {
		return ReadUnit(F, iSql);
	}

	private static JSONArray ReadUnit_root(FuncUtil F,IntegrateSql iSql,String unitid) throws Exception {
		JSONArray arr = new JSONArray();
		String unitsql = "select ComID,UnitID,UnitName,ParentID from F_Sys_Unit where ParentID=? and SysID=?";
		PrepareSql psql = new PrepareSql(iSql);
		psql.setPrepare(unitsql);
		psql.setPstValue(1, unitid);
		psql.setPstValue(2, Pl.getSysId());
		ResultSet rs = psql.executeQuery();
		while (rs.next()){
			JSONObject org = new JSONObject();
			org.put("label", rs.getString("UnitName"));
			org.put("id", rs.getString("UnitID"));
			org.put("children", ReadUnit_root(F, iSql, rs.getString("UnitID")));
			arr.add(org);
		}

		String deptsql = "select DeptID,DepartmentNumber,DepartmentName,ComID,ParentDeptID from F_Sys_Department where ComID=? and ParentDeptID='root' and SysID=?";
		PrepareSql dept_psql = new PrepareSql(iSql);
		dept_psql.setPrepare(deptsql);
		dept_psql.setPstValue(1, unitid);
		dept_psql.setPstValue(2, Pl.getSysId());
		ResultSet dept_rs = dept_psql.executeQuery();
		while (dept_rs.next()){
			JSONObject org = new JSONObject();
			org.put("label", dept_rs.getString("DepartmentName"));
			org.put("id", dept_rs.getString("DepartmentNumber"));
			org.put("children", ReadDepartment(F, iSql, unitid, dept_rs.getString("DepartmentNumber")));
			arr.add(org);
		}

		return arr;
	}

	private static JSONArray ReadUnit(FuncUtil F,IntegrateSql iSql) throws Exception {
		JSONArray arr = new JSONArray();
		String unitsql = "select ComID,UnitID,UnitName,ParentID from F_Sys_Unit where ParentID='root' and SysID=?";
		PrepareSql psql = new PrepareSql(iSql);
		psql.setPrepare(unitsql);
		psql.setPstValue(1, Pl.getSysId());
		ResultSet rs = psql.executeQuery();
		while (rs.next()){
			JSONObject org = new JSONObject();
			org.put("label", rs.getString("UnitName"));
			org.put("id", rs.getString("UnitID"));
			org.put("children", ReadUnit_root(F, iSql, rs.getString("UnitID")));
			arr.add(org);
		}
		return arr;
	}

	private static JSONArray ReadDepartment(FuncUtil F,IntegrateSql iSql, String comid, String deptid) throws Exception {
		JSONArray arr = new JSONArray();
		String unitsql = "select DeptID,DepartmentNumber,DepartmentName,ComID,ParentDeptID from F_Sys_Department where ComID=? and ParentDeptID=? and SysID=?";
		PrepareSql psql = new PrepareSql(iSql);
		psql.setPrepare(unitsql);
		psql.setPstValue(1, comid);
		psql.setPstValue(2, deptid);
		psql.setPstValue(3, Pl.getSysId());
		ResultSet rs = psql.executeQuery();
		while (rs.next()){
			JSONObject org = new JSONObject();
			org.put("label", rs.getString("DepartmentName"));
			org.put("id", rs.getString("DepartmentNumber"));
			org.put("children", ReadDepartment(F, iSql, comid, rs.getString("DepartmentNumber")));
			arr.add(org);
		}

		String persql = "select PersonID,PersonNumber,PersonName,ComID,DeptID from F_Sys_Personnel where ComID=? and DeptID=? and SysID=?";
		PrepareSql perpsql = new PrepareSql(iSql);
		perpsql.setPrepare(persql);
		perpsql.setPstValue(1, comid);
		perpsql.setPstValue(2, deptid);
		perpsql.setPstValue(3, Pl.getSysId());
		ResultSet perrs = perpsql.executeQuery();
		while (perrs.next()){
			JSONObject org = new JSONObject();
			org.put("id", perrs.getString("PersonNumber"));
			org.put("label", perrs.getString("PersonName"));
			arr.add(org);
		}

		return arr;
	}
	
	/*
	private static JSONArray ReadPersonnel(FuncUtil F,IntegrateSql iSql, String comid, String key) throws Exception {
		JSONArray arr = new JSONArray();
		String persql = "select PersonID,PersonNumber,PersonName,ComID,DeptID from F_Sys_Personnel where ComID=? and DeptID=? and SysID=?";
		PrepareSql perpsql = new PrepareSql(iSql);
		perpsql.setPrepare(persql);
		perpsql.setPstValue(1, comid);
		perpsql.setPstValue(2, key);
		perpsql.setPstValue(3, Pl.getSysId());
		ResultSet perrs = perpsql.executeQuery();
		while (perrs.next()){
			JSONObject org = new JSONObject();
			org.put("id", perrs.getString("PersonName"));
			org.put("value", perrs.getString("PersonNumber"));
			arr.add(org);
		}
		return arr;
	}*/
}
